package com.shiguo365.market.fragment;

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.shiguo365.market.R;
import com.shiguo365.market.adapter.LitemAdapter;
import com.shiguo365.market.model.Litem;
import com.yalantis.phoenix.PullToRefreshView;


import java.util.ArrayList;
import java.util.List;

/**
 * Created by caomenglong on 16/7/7.
 */
public class CenterFragment extends Fragment {
    private List<Litem> litemList=new ArrayList<Litem>();
    PullToRefreshView mPullToRefreshView;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_center, container, false);
        initLitem();
        LitemAdapter litemAdapter = new LitemAdapter(this.getActivity(),R.layout.item_litem,litemList);
        ListView listView1 =(ListView) view.findViewById(R.id.listViewCenter);
        listView1.setAdapter(litemAdapter);
        setListViewHeightBasedOnChildren(listView1);

        //*********刷新功能



        mPullToRefreshView = (PullToRefreshView) view.findViewById(R.id.pull_to_refresh);
        mPullToRefreshView.setOnRefreshListener(new PullToRefreshView.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mPullToRefreshView.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        toast("刷新完成！");
                        mPullToRefreshView.setRefreshing(false);
                    }
                }, 2000);
            }
        });

        return view;
    }

    //初始化商品列表模拟
    private void initLitem(){
        Litem litem = new Litem();
        litem.setCode(1001);
        litem.setName("我的订单");
        litem.setHitNumber(12);
        litemList.add(litem);

        litem = new Litem();
        litem.setCode(1002);
        litem.setName("世果代购");
        litem.setRemark("帮助您代购其他水果");
        litemList.add(litem);

        litem = new Litem();
        litem.setCode(1003);
        litem.setName("价目表");
        litem.setRemark("查看世果商品报价");
        litemList.add(litem);

        litem = new Litem();
        litem.setCode(1004);
        litem.setName("退货/换货");
        litem.setHitNumber(0);
        litemList.add(litem);

        litem = new Litem();
        litem.setCode(1005);
        litem.setName("地址管理");
        litemList.add(litem);

        litem = new Litem();
        litem.setCode(1006);
        litem.setName("服务建议");
        litem.setRemark("我们的进步来自您的建议");
        litemList.add(litem);


        litem = new Litem();
        litem.setCode(1007);
        litem.setName("设置");
        litemList.add(litem);

    }


    /**
     * 动态设置ListView的高度
     * @param listView
     */
    public static void setListViewHeightBasedOnChildren(ListView listView) {
        if(listView == null) return;
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            // pre-condition
            return;
        }
        int totalHeight = 0;
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }
        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        listView.setLayoutParams(params);
    }

    private void toast(String str) {
        Toast.makeText(this.getActivity(), str, Toast.LENGTH_SHORT).show();
    }
}
